import os
import sys
import django
from pathlib import Path

# 将项目根目录添加到Python路径
BASE_DIR = Path(__file__).resolve().parent.parent
sys.path.append(str(BASE_DIR))

# 设置Django环境
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
django.setup()

from users.models import User

# 创建测试用户
test_users = [
    {
        'username': 'testuser1',
        'email': 'test1@example.com',
        'password': 'testpass123',
        'role': 'user'
    },
    {
        'username': 'testuser2',
        'email': 'test2@example.com',
        'password': 'testpass123',
        'role': 'user'
    },
    {
        'username': 'admin',
        'email': 'admin@example.com',
        'password': 'admin123',
        'role': 'admin',
        'is_staff': True,
        'is_superuser': True
    }
]

def main():
    for user_data in test_users:
        try:
            # 检查用户是否已存在
            if not User.objects.filter(username=user_data['username']).exists():
                User.objects.create_user(**user_data)
                print(f"Created user: {user_data['username']}")
            else:
                print(f"User {user_data['username']} already exists")
        except Exception as e:
            print(f"Error creating user {user_data['username']}: {e}")

if __name__ == '__main__':
    main()